<?php
/**
 * @file fashionista.php
 * @author CaoQiCheng
 * @date 2017年2月8日 下午17:05:11
 * @brief 
 */
namespace haibao\jiepai\business;
use haibao\cms\model\data\Celebrity;
use haibao\frame\data\query\Condition as Condition;
use haibao\cms\model\filter\Celebrity as CelebrityFilter;
use haibao\cms\model\filter\CelebrityInfo as CelebrityInfoFilter;
use haibao\jiepai\data\Fashionista as FashionistaData;
class Fashionista extends \haibao\jiepai\business\Base{

    /**
     * 用户是否已关注明星
     * @param $fashionstaId
     * @param $userId
     * @return int 1已关注，0未关注
     */
    public static function checkUserFollow($fashionstaId,$userId){
        $data = new FashionistaData();
        return $data->checkUserFollow($fashionstaId,$userId);
    }

    /**
     * 添加关注
     * @param $models
     * @param $fashionstaIds
     */
    public static function addMarkFashionSta($models,$fashionstaIds){
        $data = new FashionistaData();
        $data->addAll($models);

        $data->updateCacheFashioinStaMarkCount($fashionstaIds);
    }

    /**
     * 取消关注
     * @param $fashionStaIds
     * @param $userId
     * @param $isUpdateCache
     */
    public static function delMarkFashionSta($fashionStaIds,$userId,$isUpdateCache = true){
        $data = new FashionistaData();
        $data->delMarkFashionSta($fashionStaIds,$userId,$isUpdateCache);
    }

    public static function getCacheFashionstaMarkCount($fashionStaId){
        $data = new FashionistaData();
        return $data->getCacheFashionstaMarkCount($fashionStaId);
    }

    /**
     * 得明星属性
     * @param $celebrityId
     * @param $selArr
     * @return mixed|null
     */
    public static function getCelebrityInfoModel($celebrityId,$selArr){
        $celebrityInfoFilter = new CelebrityInfoFilter();
        $celebrityInfoFilter->select($selArr);

        $celebrityInfoFilter->where(CelebrityInfoFilter::CONDITION_FIELD_NAME_CELEBRITY_ID, Condition::CONDITION_EQUAL, $celebrityId);

        return \haibao\cms\business\Celebrity::getOne($celebrityInfoFilter);
    }

    public static function getFashionistaTopList($limit = 100){
        $data = new FashionistaData();
        return $data->getFashionistaTopList($limit);
    }

    public static function getFashionistaTopPicture($celebrityIds){
        $data = new FashionistaData();
        return $data->getFashionistaTopPicture($celebrityIds);
    }

    /**
     * 搜索明星
     * @param array $selArr 所取内容
     * @param string $keywords 搜索内容
     * @return array|null
     */
    public static function searchCelebrityByName($selArr,$keywords){
        if(!empty($keywords)){
            $celebrityFilter = new CelebrityFilter();
            $celebrityFilter->select($selArr);

            $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN, Condition::CONDITION_OR, array(
                new Condition(CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,Condition::CONDITION_LIKE,'%'.$keywords.'%'),
                new Condition(CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,Condition::CONDITION_LIKE,'%'.$keywords.'%'),
                new Condition(CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_CH,Condition::CONDITION_LIKE,'%'.$keywords.'%'),
                new Condition(CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN,Condition::CONDITION_LIKE,'%'.$keywords.'%')));

            return self::getCelebrityList($celebrityFilter);
        }
        return null;
    }

    /**
     * 得明星信息（包含了明星头像，如果明星ID不是数组，则返回一个明星实体）
     * @param array $celebrityIds   明星ID
     * @param array $selArr 所取内容
     * @param bool $isSelImageUrl 是否取用户头像
     * @return array(haibao\cms\model\data\Celebrity)
     */
    public static function getCelebrityModels($celebrityIds,$selArr,$isSelImageUrl = true,$isGetPhoto = \haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE){
        $celebrityFilter = new CelebrityFilter();
        $celebrityFilter->select($selArr);
        if(is_array($celebrityIds)){
            $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $celebrityIds);
        } else {
            $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $celebrityIds);
        }

        $celebrityModels = self::getCelebrityList($celebrityFilter,$isSelImageUrl,$isGetPhoto);
        if(!is_array($celebrityIds) && $celebrityModels){
            $celebrityModels = $celebrityModels[0];
        }

        return $celebrityModels;
    }

    public static function getNewsHot($fashionstaId){
        $data = new FashionistaData();
        return $data->getNewsHot($fashionstaId);
    }

    public static function getFashionstaAlsoLike($fashionstaId){
        $data = new FashionistaData();
        return $data->getFashionstaAlsoLike($fashionstaId);
    }

    public static function getBoothInfo($fashionstaId){
        $data = new FashionistaData();
        return $data->getBoothInfo($fashionstaId);
    }

    public static function getPrivatePhotos($fashionstaId,$userId,$limit = 30,$lastObjectId = 0,$versionCompare=0){
        $data = new FashionistaData();
        return $data->getPrivatePhotos($fashionstaId,$userId,$limit,$lastObjectId,$versionCompare);
    }

    public static function getPrivatePhotosCount($fashionstaId){
        $data = new FashionistaData();
        return $data->getPrivatePhotosCount($fashionstaId);
    }

    public static function getOtherArticles($fashionstaId,$limit = 1){
        $data = new FashionistaData();
        return $data->getOtherArticles($fashionstaId,$limit);
    }

    public static function getFashionistaArticles($fashionstaId,$lastTime = 0,$limit = 30){
        $data = new FashionistaData();
        return $data->getFashionistaArticles($fashionstaId,$lastTime,$limit);
    }

    public static function getHaibaoArticleList($fashionstaId,$lastTime = 0,$limit = 30){
        $data = new FashionistaData();
        return $data->getHaibaoArticleList($fashionstaId,$lastTime,$limit);
    }

    public static function getFashionistaNameList($userId,$typeId,$type = '',$format = 0,$area = '',$starTypes = ''){
        $celebrityFilter = new CelebrityFilter();
        $celebrityFilter->select(array(
            CelebrityFilter::CONDITION_FIELD_NAME_ID,
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
            CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN,
        ));

        if($type == 'area'){
            if($typeId > 0){
                $areaData = self::getArea();
                if(!empty($areaData[$typeId])){
                    $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_PLACE,Condition::CONDITION_IN,$areaData[$typeId]);
                }
            }
        } else if($type == 'role') {
            if($typeId > 0){
                $starTypeData = self::getRole();
                if(!empty($starTypeData[$typeId])){
                    $starTypeArr = array();
                    foreach($starTypeData[$typeId] as $value){
                        $starTypeArr[] = new Condition(CelebrityFilter::CONDITION_FIELD_NAME_STAR_TYPES,Condition::CONDITION_LIKE,'%['.$value.']%');
                    }
                    $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,$starTypeArr);
                }
            }
        }
        if($area){
            $areaData = self::getArea();
            if(!empty($areaData[$area])){
                $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_PLACE,Condition::CONDITION_IN,$areaData[$area]);
            }
        }

        if($starTypes){
            $starTypeData = self::getRole();
            if(!empty($starTypeData[$starTypes])){
                $starTypeArr = array();
                foreach($starTypeData[$starTypes] as $value){
                    $starTypeArr[] = new Condition(CelebrityFilter::CONDITION_FIELD_NAME_STAR_TYPES,Condition::CONDITION_LIKE,'%['.$value.']%');
                }
                $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,$starTypeArr);
            }
        }
        $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Celebrity::STATUS_ENABLE);
        $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE,Condition::CONDITION_EQUAL,1);

        $starList = self::getCelebrityList($celebrityFilter);
        $newList = $newAZList = array();
        if($starList){
            $fashionstaIds = array();
            foreach ($starList as $star){
                if($star->Pinyin_en){
                    $fashionstaIds[] = $star->Id;
                }
            }
            $followIds = array();
            $data = new FashionistaData();
            if($userId && $fashionstaIds){
                $followIds = $data->getFollwFashionList($fashionstaIds,$userId);
            }

            foreach ($starList as $star){
                if($star->Pinyin_en){
                    $isFollow = 0;
                    if(in_array($star->Id,$followIds)){
                        $isFollow = 1;
                    }

                    $picUrl = $star->ImageSrc;
                    if (strpos($picUrl, '/jiepai-1/') === false) {
                        $picUrl = '/jiepai-1/' . ltrim($picUrl, '/');
                    }
                    $star->ImageSrc =  \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\jiepai\Config::CDN_URL_IMAGE).ltrim($picUrl, '/');

                    $newAZList[$star->Pinyin_en][] = array('FashionistaID'=>$star->Id,'Name'=>$star->NameCn,'NameFr'=>$star->NameEn,'Avatar'=>$star->ImageSrc,'IsFollow'=>$isFollow,'FirstChar'=>$star->Pinyin_en,'ClickType'=>'fashionistaList');
                    $newList[] = array('FashionistaID'=>$star->Id,'Name'=>$star->NameCn,'NameFr'=>$star->NameEn,'Avatar'=>$star->ImageSrc,'IsFollow'=>$isFollow,'FirstChar'=>$star->Pinyin_en,'ClickType'=>'fashionistaList');
                }
            }


            if($format == 1){
                $newList = array_values($newList);
            } else {
                $newList = array();
                ksort($newAZList);
                foreach ($newAZList as $key=>$star){
                    $newList[] = array('name'=>$key,'personList'=>$star);
                }
            }
        }
        return $newList;
    }

    private static function getCelebrityList($celebrityFilter,$isSelImageUrl = true,$isGetPhoto = \haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE){
        $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE, Condition::CONDITION_EQUAL, 1);
        if($isSelImageUrl){
            $celebrityImageFilter = new \haibao\cms\model\filter\CelebrityImage();
            $celebrityImageFilter->select(array(
                $celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,
                $celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE
            ));
            if($isGetPhoto == \haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_ICON){
                $imageTypeArr = array(
                    new Condition($celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_ICON),
                    new Condition($celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_SCREEN)
                );
                $celebrityImageFilter->where($celebrityImageFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,$imageTypeArr);
                $celebrityFilter->leftjoin($celebrityImageFilter,null,$celebrityFilter::CONDITION_FIELD_NAME_ID,$celebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,$celebrityFilter::CONDITION_FIELD_NAME_CELEBRITY_IMAGE,true);
            } else {
                $celebrityImageFilter->where($celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE);
                $celebrityFilter->leftjoin($celebrityImageFilter,null,$celebrityFilter::CONDITION_FIELD_NAME_ID,$celebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,$celebrityFilter::CONDITION_FIELD_NAME_CELEBRITY_IMAGE,false);
            }
            $imageFilter = new \haibao\cms\model\filter\ImageLibrary();
            $imageFilter->select(array(
                $imageFilter::CONDITION_FIELD_NAME_FILENAME
            ));
            $celebrityFilter->leftjoin($imageFilter,$celebrityImageFilter,$celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,$imageFilter::CONDITION_FIELD_NAME_ID,$celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_Url,false);

        }

        $celebrityModels = \haibao\cms\business\Celebrity::getAll($celebrityFilter);
        if($celebrityModels) {
            foreach ($celebrityModels as $key=>$model) {
                //明星头像
                $model->ImageSrc = '';
                if($isSelImageUrl){
                    if($isGetPhoto == \haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_ICON){
                        if (!empty($model->CelebrityImage)) {
                            foreach($model->CelebrityImage as $celebrityImage){
                                $model->ImageSrc = $celebrityImage->ImageUrl->Filename;
                                if($celebrityImage->ImageType == $isGetPhoto){
                                    break;
                                }
                            }
                        }
                    } else {
                        if (!empty($model->CelebrityImage) && !empty($model->CelebrityImage->ImageUrl)) {
                            $model->ImageSrc = $model->CelebrityImage->ImageUrl->Filename;
                        }
                    }
                }
				
                //身份/职业
                if(!empty($model->StarTypes)){
                    $starTypes = explode(',',$model->StarTypes);
                    $temp = array();
                    $typeArr = self::$defaultType;
                    foreach($starTypes as $type){
                        if(!empty($typeArr[preg_replace('/\[|\]/i','',$type)])){
                            $temp[] = $typeArr[preg_replace('/\[|\]/i','',$type)];
                        }
                    }
                    $model->StarTypes = $temp;
                }

                //地域
                if(!empty($model->Place)){
                    $placeArr = explode(',',$model->Place);
                    $defaultPlace = self::$defaultPlace;
                    $placeNames = array();
                    foreach ($placeArr as $place){
                        if(!empty($defaultPlace[$place])){
                            $placeNames[] = $defaultPlace[$place];
                        }
                    }
                    $model->Place = $placeNames;
                } 

                $celebrityModels[$key] = $model;
            }
        }

        return $celebrityModels;
    }

    public static function getRole(){
        return array(
            1=>array(2,4,5,12),
            2=>array(6),
            3=>array(3,7,8)
        );
    }
    //获取达人积分最高的四篇文章
    public static function getTopFourJiePaiArticles($TagId){
        $data = new FashionistaData();
        return $data->getTopFourJiePaiArticles($TagId);
    }
    
    public static function getRoleExtraInfo($TagId){
       $selArr = array(
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_STAR_TYPES,
        );
        $data = self::getCelebrityModels($TagId, $selArr);
        $starType = array();
        if($data && count($data->StarTypes)>0){
            $starType = $data->StarTypes;
        }
        return $starType;
    }
    
    public static function getBasicExtraInfo($TagId){
        $selArr1 = array(
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_DESCRIPTION,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_PLACE,
        );
        $selArr2 = array(
            \haibao\cms\model\filter\CelebrityInfo::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\CelebrityInfo::CONDITION_FIELD_NAME_SUCCESS,
        );
        $dataInfo = array();
        $data = self::getCelebrityModels($TagId, $selArr1);
        if($data){
            $dataInfo['Country'] = $data->Place?$data->Place:array();
            $dataInfo['Introduction'] = strip_tags($data->Description);
        }
        $dataSuccess = self::getCelebrityInfoModel($TagId, $selArr2);
        if($dataSuccess){
            $dataInfo['ShortIntroduction'] = strip_tags($dataSuccess->Success);
        }
        return $dataInfo;
    }

    public static function getArea(){
        return array(
            1=>array(1),
            2=>array(2,3),
            3=>array(5),
            4=>array(4,6)
        );
    }
    
    public static function getBasicInfo($highestPerson){

//         $oldHighestPerson = $highestPerson;
//         $data = new FashionistaData();
//         $dataMap = $data->getCmsDataMap($highestPerson, 2);

//         if(!$dataMap) return array();

//         $highestPerson = $dataMap['mapId'];

        $selArr = array(
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_ID,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_PLACE,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_CN,
            \haibao\cms\model\filter\Celebrity::CONDITION_FIELD_NAME_NAME_EN,
        );
        $data = self::getCelebrityModels($highestPerson, $selArr,true);
        $starType = array();
        if($data){
            $starType = array('FashionistaID'=>$data->Id,
                'DevelopArea'=>isset($data->Place[0])?$data->Place[0]:'',
                'Name'=>$data->NameEn,
                'NameFr'=>$data->NameCn,
                'Avatar'=>$data->ImageSrc,
                'AvatarCrop' =>0,
                'Portrait' => $data->ImageSrc,
                'PortraitCrop'=>0
            );
        }

        return $starType;
    }

    public static function getHotRecommendFashionistas($userId,$limit = null){
        $data = new FashionistaData();
        return $data->getHotRecommendFashionistas($userId,$limit);
    }

    public static function isFashionista($celebrityId){
        $data = new FashionistaData();
        return $data->isFashionista($celebrityId);
    }
}